package com.naiterui.ehp.bs.cms.modules.sys.service.impl;

import java.util.List;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.constants.CacheConstants;
import com.naiterui.ehp.bs.cms.common.entity.BaseTree;
import com.naiterui.ehp.bs.cms.modules.sys.entity.Dept;
import com.naiterui.ehp.bs.cms.modules.sys.mapper.DeptMapper;
import com.naiterui.ehp.bs.cms.modules.sys.service.IDeptService;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Override
    @Cacheable(value = CacheConstants.DEPT_LIST)
    public List<Dept> queryList() {
        List<Dept> deptList = this.list(new QueryWrapper<>());
        return BaseTree.listToTree(deptList);
    }

    @Override
    public List<Dept> select() {
        List<Dept> deptList = this.baseMapper.getAllSelect();
        // 添加一级部门
        Dept root = new Dept();
        root.setId(-1L);
        root.setName("顶级部门");
        root.setParentId(-2L);
        deptList.add(root);
        return BaseTree.listToTree(deptList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstants.DEPT_LIST, allEntries = true)
    public boolean save(Dept dept) {
        return super.save(dept);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstants.DEPT_LIST, allEntries = true)
    public boolean updateById(Dept dept) {
        return super.updateById(dept);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = CacheConstants.DEPT_LIST, allEntries = true)
    public void delete(long id) throws BusinessException {
        // 判断是否有子部门
        List<Long> deptList = this.baseMapper.queryDetpIdList(id);
        if (!CollectionUtils.isEmpty(deptList)) {
            throw new BusinessException(BaseExceptionCodes.FAILED, "请先删除子部门");
        }
        super.removeById(id);
    }

    @Override
    public List<Long> getIdByPatientId(Long patientId) {
        List<Long> sonIds = Lists.newArrayList(patientId);
        this.findSon(patientId, sonIds);
        return sonIds;
    }

    private void findSon(Long patientId, List<Long> deptIds) {
        List<Long> detpIdList = this.baseMapper.queryDetpIdList(patientId);
        if (!CollectionUtils.isEmpty(detpIdList)) {
            deptIds.addAll(detpIdList);
        }
        detpIdList.forEach(aLong -> this.findSon(aLong, deptIds));
    }


}
